//
//  ShopOneCollectionViewController.m
//  1243
//
//  Created by 杨森 on 16/4/27.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ShopOneCollectionViewController.h"
#import "ShopOneCollectionViewCell.h"
#import "XRCarouselView.h"
#import "JSDropDownMenu.h"

#import "ShopDetailsViewController.h"
#import "XLPlainFlowLayout.h"

#define MENU_HIGH 45

@interface ShopOneCollectionViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,XRCarouselViewDelegate,JSDropDownMenuDataSource,JSDropDownMenuDelegate>
{
    
    NSMutableArray *_data1;
    NSMutableArray *_data2;
    NSMutableArray *_data3;
    
    NSInteger _currentData1Index;
    NSInteger _currentData2Index;
    NSInteger _currentData3Index;
}

@property(nonatomic,strong)UICollectionViewFlowLayout *layout;

@property(nonatomic,strong)UICollectionView *ShopCollectionView;

/**
 *  轮播
 */
@property(nonatomic,strong)XRCarouselView *XRCarouselView;

/**
 *  筛选
 */
@property (nonatomic, strong) JSDropDownMenu *menu;

@end

@implementation ShopOneCollectionViewController

static NSString * const reuseIdentifier = @"shopOneCell";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addCollectView];
   
    [self addBackBtn];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self addHeaderView];
}

/**
 *  添加返回按钮
 */
- (void)addBackBtn
{
    UIView *backBgView = [[UIView alloc] initWithFrame:CGRectMake(WEITH/6*5, HEIGHT/5*4, 30, 30)];
    [self.view addSubview:backBgView];
    
    backBgView.layer.borderWidth = 1;
    backBgView.layer.borderColor = COLOR(128, 128, 128, 1).CGColor;
    backBgView.backgroundColor = [UIColor blackColor];
    backBgView.alpha = 0.6;
    
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(WEITH/6*5 + 5,  HEIGHT/5*4 +5, 20, 20);
    [self.view addSubview:button];
    [button setImage:[UIImage imageNamed:@"backTop"] forState:UIControlStateNormal];
//    [button setImage:[UIImage imageNamed:@"backTop"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)backBtnClick
{
    NSIndexPath *firstIndex = [NSIndexPath indexPathForItem:0 inSection:1];
    [self.ShopCollectionView selectItemAtIndexPath:firstIndex animated:YES scrollPosition:UICollectionViewScrollPositionTop];
}


- (void)addCollectView
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    CGFloat margin = 5;
    XLPlainFlowLayout *flowlayout = [[XLPlainFlowLayout alloc] init];
    flowlayout.itemSize = CGSizeMake(WEITH/2 - margin/2,HEIGHT/2.5 + 20);
    flowlayout.minimumLineSpacing = margin;
    flowlayout.minimumInteritemSpacing = margin;
    flowlayout.naviHeight = 0;
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView * ShopCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WEITH, HEIGHT) collectionViewLayout:flowlayout];

    self.ShopCollectionView = ShopCollectionView;
    self.ShopCollectionView.delegate = self;
    self.ShopCollectionView.dataSource = self;
    self.ShopCollectionView.backgroundColor = BACKGROUND_COLOR;
    [self.view addSubview:self.ShopCollectionView];
    
    [self.ShopCollectionView registerNib:[UINib nibWithNibName:@"ShopOneCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:reuseIdentifier];
    //注册表头
    [self.ShopCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];


}


-(void) addHeaderView
{
    //轮播
    NSArray *imageArr = @[
                          [UIImage imageNamed:@"fjsj_banner1_02"],
                          [UIImage imageNamed:@"fjsj_banner2_02"],
                          [UIImage imageNamed:@"fjsj_banner3_02"]
                          ];
    
    self.XRCarouselView = [XRCarouselView carouselViewWithFrame:CGRectMake(0,0, WIDTH, HEIGHT/4) imageArray:imageArr];
    
    _XRCarouselView.delegate = self;
    _XRCarouselView.time = 2;
    
}

- (void)creatHeaderView:(UICollectionReusableView *)view {
    
    NSArray *arr = @[@"综合", @"销量", @"价格"];
    for (int i = 0; i < 3; i ++) {
        UIButton *btn = [[UIButton alloc] init];
        
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        btn.layer.masksToBounds = YES;
        btn.layer.borderColor  = [UIColor blackColor].CGColor; //要设置的颜色
        btn.layer.borderWidth = 0.3; //要设置的描边宽
        
        btn.tag = 1000 + i;
        
        [btn setFrame:CGRectMake(WIDTH / 3 * i, 0, WIDTH / 3, 44)];
        
        [view addSubview:btn];
    }
}


#pragma mark - JSDropDownMenuDataSource
- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    
    return 3;
}

-(BOOL)displayByCollectionViewInColumn:(NSInteger)column{
    
    if (column==2) {
        
        return YES;
    }
    
    return NO;
}

-(BOOL)haveRightTableViewInColumn:(NSInteger)column{
    
    if (column == 0 || column == 1) {
        return YES;
    }
    return NO;
}

-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{
    
    if (column==0) {
        return 0.3;
    }
    
    return 1;
}

-(NSInteger)currentLeftSelectedRow:(NSInteger)column{
    
    if (column==0) {
        
        return _currentData1Index;
        
    }
    if (column==1) {
        
        return _currentData2Index;
    }
    
    return 0;
}

- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{
    
    if (column==0) {
        if (leftOrRight==0) {
            
            return _data1.count;
        } else{
            
            NSDictionary *menuDic = [_data1 objectAtIndex:leftRow];
            return [[menuDic objectForKey:@"data"] count];
        }
    } else if (column==1){
        
        return _data2.count;
        
    } else if (column==2){
        
        return _data3.count;
    }
    
    return 0;
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{
    
    switch (column) {
        case 0: return [[_data1[0] objectForKey:@"data"] objectAtIndex:0];
            break;
        case 1: return _data2[0];
            break;
        case 2: return _data3[0];
            break;
        default:
            return nil;
            break;
    }
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column==0) {
        if (indexPath.leftOrRight==0) {
            NSDictionary *menuDic = [_data1 objectAtIndex:indexPath.row];
            return [menuDic objectForKey:@"title"];
        } else{
            NSInteger leftRow = indexPath.leftRow;
            NSDictionary *menuDic = [_data1 objectAtIndex:leftRow];
            return [[menuDic objectForKey:@"data"] objectAtIndex:indexPath.row];
        }
    } else if (indexPath.column==1) {
        
        return _data2[indexPath.row];
        
    } else {
        
        return _data3[indexPath.row];
    }
}

#pragma mark - JSDropDownMenuDelegate
- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column == 0) {
        
        if(indexPath.leftOrRight==0){
            
            _currentData1Index = indexPath.row;
            
            return;
        }
        
    } else if(indexPath.column == 1){
        
        _currentData2Index = indexPath.row;
        
    } else{
        
        _currentData3Index = indexPath.row;
    }
}


#pragma mark - XRCarouselViewDelegate
- (void)carouselView:(XRCarouselView *)carouselView didClickImage:(NSInteger)index {
    NSLog(@"点击了第%ld张图片", index);
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ShopOneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.ShopImage.image = [UIImage imageNamed:@"shopZan"];
//    cell.ShopImage.contentMode = UIViewContentModeScaleAspectFit;
    cell.shopNameLabel.text = @"爱疯手机9Plus 大放送";
    cell.saleLabel.text = @"已售9999";
    cell.priceLabel.text = @"9999";
    
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    ShopDetailsViewController *detailVC = [[ShopDetailsViewController alloc] init];
//    [self.navigationController pushViewController:detailVC animated:YES];
//    NSLog(@"%zd",indexPath.row);
}

#pragma mark---加载UICollectionView表头
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    if ([collectionView isEqual:self.ShopCollectionView]) {
        
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { // header
            
            if (indexPath.section == 0) {
               reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];

                [reusableView addSubview:self.XRCarouselView];
                return reusableView;
            } else {
                reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
                [self creatHeaderView:reusableView];
                reusableView.backgroundColor = [UIColor whiteColor];
                return reusableView;
            }
        }
        
    }
 
    return nil;
    
}

//header组头大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(WEITH,HEIGHT/4+10);
    }
    
    CGSize size={WEITH,MENU_HIGH};
    return size;
}


//tabBar 隐藏
- (void)pushForm:(UINavigationController *)pushForm pushToAndHidden:(UIViewController *)pushToVC
{
    [pushToVC setHidesBottomBarWhenPushed:YES];
    [pushForm pushViewController:pushToVC animated:YES];
    
}



@end
